// pages/qualityControl/equipmentManage/addEquipment/index.js
import WxValidate from '../../../../utils/validate'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    winHeight: "",
    array3: [],
    value3: 0,
    oldlength: 0,
    type: ['电离室', '剂量仪'],
    serialOrLotNumber: "",
    typeName: "",
    typeValue: "",
    ifchange: false,
    code: "",
    model: "",
    id: "",
    reportCode: "",
    calibrationDate: "",
    dueDate: "",
    pressure: "",
    temperature: "",
    calibrationFactor: "",
    access: "",
    num: "",
    qualityControlEquipmentId: ""
  },
  bindPicker3Change: function (e) {
    this.setData({
      serialOrLotNumber: this.data.array3[e.detail.value]
    })
  },
  initValidate() {
    var rules = {}
    var messages = {}
    rules = {
      serialOrLotNumber: {
        required: true,
      },
      typeName: {
        required: true,
      },
      code: {
        required: true,
      },
      calibrationDate: {
        required: true,
      },
      dueDate: {
        required: true,
      },
      pressure: {
        required: true,
      },
      temperature: {
        required: true,
      },

    }
    messages = {

      serialOrLotNumber: {
        required: '请填写关联设备',
      },
      typeName: {
        required: '请填写种类',
      },
      code: {
        required: '请填设备编号',
      },
      calibrationDate: {
        required: '请填写校准时间',
      },
      dueDate: {
        required: '请填写过期时间',
      },
      pressure: {
        required: '请填写参考气压',
      },
      temperature: {
        required: '请填写参考温度',
      },
    }
    // let that = this;
    // this.WxValidate = new WxValidate(rules, messages)
    // this.WxValidate.addMethod('limitInput',(value, param)=>{
    //   return  this.WxValidate.optional(value) || /(^[0-9]*$)/.test(value);
    // },'校准因子为数字')

    let that = this;
    this.WxValidate = new WxValidate(rules, messages)
  },
  // 展示信息
  showModal(error) {
    wx.showModal({
      content: error.msg,
      showCancel: false,
    })
  },
  bindDateChange: function (e) {
    this.setData({
      calibrationDate: e.detail.value
    })
  },
  bindDateChangetwo: function (e) {
    this.setData({
      dueDate: e.detail.value
    })
  },
  typeChange(e) {
    this.setData({
      typeName: this.data.type[e.detail.value]
    })
    if (this.data.typeName == "电离室") {
      this.setData({
        typeValue: 0
      })
    } else if (this.data.typeName == "剂量仪") {
      this.setData({
        typeValue: 1
      })
    }
  },
  codechange(e) {
    this.setData({
      code: e.detail.value
    })
  },
  modelchange(e) {
    this.setData({
      model: e.detail.value
    })
  },
  reportCodechange(e) {
    this.setData({
      reportCode: e.detail.value
    })
  },
  pressurechange(e) {
    this.setData({
      pressure: e.detail.value
    })
  },
  temperaturechange(e) {
    this.setData({
      temperature: e.detail.value
    })
  },
  numchange(e) {
    if (this.data.oldlength == parseInt(e.detail.value)) {
      this.data.ifchange = false
    } else {
      this.data.ifchange = true
      let obj = {
        access: "", calibrationFactor: ""
      }
      let arr = []
      for (let i = 0; i < parseInt(e.detail.value); i++) {
        arr.push(obj)
      }
      this.setData({
        num: parseInt(e.detail.value),
        extendList: arr
      })
    }

  },
  accesschange(e) {
    if (this.data.ifchange) {
      console.log(e)
      let index = Number(e.currentTarget.dataset.item);
      let temp = this.data.extendList[e.currentTarget.dataset.item].calibrationFactor
      this.data.extendList[index] = { access: e.detail.value, calibrationFactor: temp }
      console.log(this.data.extendList)
    } else {
      console.log(e)
      let index = Number(e.currentTarget.dataset.item);
      let temp = this.data.extendList[e.currentTarget.dataset.item].calibrationFactor
      this.data.extendList[index] = { access: e.detail.value, calibrationFactor: temp, qualityControlEquipmentId: this.data.extendList[index].qualityControlEquipmentId, id: this.data.extendList[index].id }
      console.log(this.data.extendList)
    }


  },
  calibrationFactorchange(e) {
    if (this.data.typeValue == 0) {
      this.data.extendList[0] = { calibrationFactor: e.detail.value, qualityControlEquipmentId: this.data.extendList[0].qualityControlEquipmentId, id: this.data.extendList[0].id }

    } else {
      if (this.data.ifchange) {
        let index = Number(e.currentTarget.dataset.item);
        console.log(e.currentTarget.dataset.item)
        let temp = this.data.extendList[e.currentTarget.dataset.item].access
        this.data.extendList[e.currentTarget.dataset.item] = { access: temp, calibrationFactor: e.detail.value, }
      } else {
        let index = Number(e.currentTarget.dataset.item);
        console.log(e.currentTarget.dataset.item)
        let temp = this.data.extendList[e.currentTarget.dataset.item].access
        this.data.extendList[e.currentTarget.dataset.item] = { access: temp, calibrationFactor: e.detail.value, qualityControlEquipmentId: this.data.extendList[index].qualityControlEquipmentId, id: this.data.extendList[index].id }
      }
    }


  },
  getequiment() {
    this
      .axios
      .custom("AllInstalledProducts", "serialOrLotNumbers", {
        id: this.Store.getItem("userid"),
      })
      .then((result) => {
        console.log(result);
        this.setData({
          array3: result.data
        })
      })
      .catch((err) => {
        self.$notify(err.data.msg, "error");
      });
  },
  save() {
    let self = this
    if (self.data.ifchange) {
      self.data.extendList.forEach(e => {
        e.qualityControlEquipmentId = self.data.id
      })
    }
    console.log(isNaN(self.data.pressure))
    console.log(isNaN(self.data.temperature))
    if(isNaN(self.data.pressure)){
      this.showModal({msg:"参考气压为数字"});
      return false
    }else if(isNaN(self.data.temperature)){
      this.showModal({msg:"参考温度为数字"});
      return false
    }
    let obj = {
      serialOrLotNumber: self.data.serialOrLotNumber,
      typeName: self.data.typeName,
      typeValue: self.data.typeValue,
      code: self.data.code,
      model: self.data.model,
      access: self.data.access,
      calibrationFactor: self.data.calibrationFactor,
      extendList: self.data.extendList,
      reportCode: self.data.reportCode,
      calibrationDate: self.data.calibrationDate,
      dueDate: self.data.dueDate,
      pressure: self.data.pressure,
      temperature: self.data.temperature,
      num: self.data.num,
      id: self.data.id
    }
    if (self.data.typeValue == 1) {
      let juge = true
      if (self.data.num || self.data.num > 0) {
        self.data.extendList.forEach(e => {
          if (e.access == "") {
            this.showModal({ msg: "请填入剂量仪通道" });
            juge = false
            return false
          } else if (e.calibrationFactor == "") {
            this.showModal({ msg: "请填入通道数量" });
            juge = false
            return false
          }
        })
        if (!juge) {
          return
        }
      } else {
        this.showModal({ msg: "请填入剂量仪通道" });
        return false
      }

    } else {
      if (self.data.calibrationFactor == "") {
        this.showModal({ msg: "请填入校准因子" });
        return false
      }
    }
    if (!this.WxValidate.checkForm(obj)) {
      let msg = this.WxValidate.errorList[0];
      this.showModal(msg);
      return false
    }
    self
      .axios
      .addOrUpdate("QualityControlEquipment", obj, true)
      .then(() => {
        wx.navigateBack({
          delta: 2
        })
      })
      .catch((err) => {

      });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 高度自适应
    let that = this;
    wx.getSystemInfo({
      success: function (res) {
        var clientHeight = res.windowHeight,
          clientWidth = res.windowWidth,
          rpxR = 750 / clientWidth;
        var calc = clientHeight * rpxR - 140;
        console.log(calc)
        that.setData({
          winHeight: calc
        });
      }
    });
    this.getequiment()

    let self = this
    const eventChannel = this.getOpenerEventChannel();
    // 监听 index页面定义的toHome 事件
    eventChannel.on('toequipmentEdtion', (res) => {
      this.setData({
        serialOrLotNumber: res.item.serialOrLotNumber,
        typeName: res.item.typeName,
        typeValue: res.item.typeValue,
        code: res.item.code,
        model: res.item.model,
        reportCode: res.item.reportCode,
        calibrationDate: res.item.calibrationDate,
        dueDate: res.item.dueDate,
        pressure: res.item.pressure,
        temperature: res.item.temperature,
        extendList: res.item.extendList,
        num: res.item.extendList.length,
        oldlength: res.item.extendList.length,
        calibrationFactor: res.item.extendList[0].calibrationFactor,
        access: res.item.access,
        id: res.item.id,
        // qualityControlEquipmentId:res.item.extendList[0].qualityControlEquipmentId
      })
      console.log(res, '页面传值')
      self.initValidate();
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})